package learnjava.concur.chapter6;

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;

public class P018 {

    public static void main(String[] args) {

        Student stu = new Student();
        AtomicReferenceFieldUpdater updater =
                AtomicReferenceFieldUpdater.newUpdater(Student.class,String.class,"name");
        System.out.println(updater.compareAndSet(stu,null,"张三")); //A
        System.out.println(updater.compareAndSet(stu,"张三","张三1")); //B
        System.out.println(updater.compareAndSet(stu,"张三1",null)); //A
        System.out.println(updater.compareAndSet(stu,null,"张三")); //A
    }

}

class Student{

    volatile String name;

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                '}';
    }
}
